package com.smartscreen.baseinfomanage.service.impl;

import com.smartscreen.baseinfomanage.entity.Teacher;
import com.smartscreen.baseinfomanage.mapper.TeacherMapper;
import com.smartscreen.baseinfomanage.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public List<Teacher> getAllTeachers() {
        return teacherMapper.selectList(null);
    }

    @Override
    public Teacher getTeacherById(String teacherId) {
        return teacherMapper.selectById(teacherId);
    }

    @Override
    public Teacher createTeacher(Teacher teacher) {
        teacherMapper.insert(teacher);
        return teacher;
    }

    @Override
    public Teacher updateTeacher(Teacher teacher) {
        teacherMapper.updateById(teacher);
        return teacher;
    }

    @Override
    public void deleteTeacher(String teacherId) {
        teacherMapper.deleteById(teacherId);
    }


    // ... existing code ...

    @Override
    public Map<String, Map<String, Object>> getTeachersBatchAsMap(List<String> teacherIds) {
        if (teacherIds == null || teacherIds.isEmpty()) {
            return new HashMap<>();
        }

        // 去重并过滤空值
        List<String> validIds = teacherIds.stream()
                .filter(Objects::nonNull)
                .filter(id -> !id.trim().isEmpty())
                .distinct()
                .collect(Collectors.toList());

        if (validIds.isEmpty()) {
            return new HashMap<>();
        }

        // 批量查询教师信息
        List<Teacher> teachers = teacherMapper.selectBatchIds(validIds);

        // 转换为Map格式
        Map<String, Map<String, Object>> result = new HashMap<>();
        for (Teacher teacher : teachers) {
            Map<String, Object> teacherMap = new HashMap<>();
            teacherMap.put("teacherId", teacher.getTeacherId());
            teacherMap.put("name", teacher.getName());
            teacherMap.put("createdAt", teacher.getCreatedAt());
            teacherMap.put("updatedAt", teacher.getUpdatedAt());

            result.put(teacher.getTeacherId(), teacherMap);
        }

        return result;
    }

// ... existing code ...
}